import { createRouter, createWebHashHistory } from 'vue-router'
import Home from '../views/Home.vue'


const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: Home,
    },
    {
      path: '/mine',
      name: 'mine',
      component: () => import('../views/Mine.vue'),
    },
    {
      path: '/star',
      name: 'star',
      component: () => import('../views/Star.vue')
    },
    {
      path: '/detail/:cookId',
      name: "detail",
      component: () => import('../views/DetailV2.vue')
    },
    {
      path: '/search',
      name: "search",
      component: () => import('../views/Search.vue')
    },
    {
      path: '/basket',
      name: "basket",
      component: () => import('../views/Basket.vue')
    }
  ],
  scrollBehavior: (to, from, savedPosition) => {
    return {
      top: 0
    }
  }
})

// 全局导航守卫
router.beforeEach((to, from, next) => {
  // 这里的设置可以动态改变页面的背景颜色
  if (to.name == 'home') {
    document.body.style.backgroundColor = '#ffffff';
  } else {
    document.body.style.backgroundColor = 'var(--color-background-soft)';
  }
  next()
})

export default router
